﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;
using Honestysoft.Financing.Model;

namespace Honestysoft.Financing.WebUI.Module.Audit
{
    /// <summary>
    /// SaveInvoice 的摘要说明
    /// </summary>
    public class SaveInvoice : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.ContentType = "text/plain";
            string id = context.Request.Params["ID"];
            string dataString = context.Request.Params["Data"];
            if (string.IsNullOrEmpty(dataString))
            {
                context.Response.Write("0");
            }
            else
            {
                string[] arrItem = dataString.Split(',');
                Honestysoft.Financing.BusinessLogic.Audit bll = new BusinessLogic.Audit("");
                List<Bu_AuditTable_Invoice> lsInvoice = new List<Bu_AuditTable_Invoice>();
                Bu_AuditTable_Invoice item = null;
                string[] arrInv = null;
                int sortNo = 0;
                foreach (string s in arrItem)
                {
                    arrInv = s.Split('|');
                    item = new Bu_AuditTable_Invoice();
                    item.ID = Honestysoft.BaseLibrary.Common.Guid.GetGuid();
                    item.TableID = id;
                    item.SortNo = sortNo++;
                    item.Remark = context.Server.UrlDecode(arrInv[4]);
                    item.IsChecked = arrInv[3] == "√";
                    item.InvoiceNo = arrInv[0];
                    item.InvoiceCode = arrInv[1];
                    item.Amount = Convert.ToDecimal(arrInv[2]);
                    lsInvoice.Add(item);
                }
                if (bll.SaveInvoices(id, lsInvoice))
                {
                    DataTable dt = null;
                    List<DataTable> lsDT = new List<DataTable>();
                    foreach (string s in arrItem)
                    {
                        dt = bll.GetExistsInvoice(id, s.Split('|')[0], s.Split('|')[1]);
                        if (dt != null && dt.Rows.Count > 0)
                        {
                            lsDT.Add(dt);
                        }
                    }
                    if (lsDT.Count > 0)
                    {
                        string resData = "下列发票已存在使用记录：\r\n";
                        for (int i = 0; i < lsDT.Count; i++)
                        {
                            resData += (i + 1).ToString() + "、" + lsDT[i].Rows[0]["Code"].ToString() + "：发票号码[" + lsDT[i].Rows[0]["InvoiceNo"].ToString() + "]，发票代码[" + lsDT[i].Rows[0]["InvoiceCode"].ToString() + "]，备注[" + lsDT[i].Rows[0]["Remark"].ToString() + "]\r\n";
                        }
                        context.Response.Write(context.Server.HtmlEncode(resData));
                    }
                    else
                    {
                        context.Response.Write("0");
                    }                    
                }
                else
                {
                    context.Response.Write(context.Server.HtmlEncode("检查发票信息时出错！"));
                }                
            }
            //检查是否已经使用了此发票
            
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}